<?php

namespace app\admin\controller;

use app\common\controller\Backend;


class User extends Backend
{
    //用户处理
    public function userlist()
    {
        $where = null;
        $userList = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $name = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';

            if ($name == 'nickname') {
                $where = "1";
                $userList = model('User.User')->where('nickname', 'like', "%$keyword%")->paginate(7);
            }
            if ($name == 'email') {
                $where = "1";
                $userList = model('User.User')->where('email', 'like', "%$keyword%")->paginate(7);
            }
            if ($where == null) {
                $userList = model('User.User')->paginate(7);
            }
        }
        $navTitle = '用户管理';
        $navSmall = '用户列表';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'userList' => $userList
        ]);
    }

    //回收站处理
    public function recyclebin()
    {
        //恢复用户数据
        if ($this->request->isPost() && $this->request->isAjax()) {
            $action = $this->request->param('action', '');
            if ($action == 'restore') {
                $list = $this->request->param();
                $deletetimeList = model('User.User')->onlyTrashed()->select($list['list']);
                $result = null;
                foreach ($deletetimeList as $key => $item) {
                    // halt($item['id']);
                    $result = model('User.User')->update(['id' => $item['id'], 'deletetime' => null]);
                }
                // $data = [];
                // foreach ($deletetimeList as $key => $item) {
                //     $data[$key]['id'] = $item['id'];
                //     $data[$key]['deteletime'] = null;
                // }
                // $result = model('User.User')->isUpdate(true)->saveAll($data);
                if ($result === FALSE) {
                    $this->error(model('User.User')->getError());
                } else {
                    $this->success('恢复成功');
                }
            }
        }

        $where = null;
        $userList = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $name = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';

            if ($name == 'nickname') {
                $where = "1";
                $userList = model('User.User')->onlyTrashed()->where('nickname', 'like', "%$keyword%")->paginate(7);
                // echo model('Admin')->getlastsql();
                // exit;
            }
            if ($name == 'email') {
                $where = "1";
                $userList = model('User.User')->onlyTrashed()->where('email', 'like', "%$keyword%")->paginate(7);
            }
            if ($where == null) {
                $userList = model('User.User')->onlyTrashed()->paginate(7);
            }
        }

        // $userList = model('User.User')->onlyTrashed()->paginate(7);
        $navTitle = '用户管理';
        $navSmall = '回收站';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'userList' => $userList
        ]);
    }

    //积分处理
    public function record()
    {

        $userid = $this->request->param('userid', 0);
        // $page = $this->request->param('page', 0);
        // halt($page);
        $where = null;
        $recordList = null;
        if ($this->request->isGet()) {
            $params = $this->request->param();
            $name = !empty($params['search_field']) ? $params['search_field'] : '';
            $keyword = !empty($params['keyword']) ? $params['keyword'] : '';

            if (!empty($keyword)) {
                $where = "1";
                $recordList = model('Record')->where('content', 'like', "%$keyword%")->paginate(7, false, [
                    'query' => ['keyword' => $keyword],
                ]);
            }

            if ($where == null) {
                if ($userid == 0) {
                    $recordList = model('Record')->with(['user'])->paginate(7);
                    // echo model('Record')->getlastsql();
                    // exit;
                } else {
                    $recordList = model('Record')->with(['user'])->where('userid', $userid)->paginate(7);
                }
            }
        }


        $navTitle = '用户管理';
        $navSmall = '用户积分记录';
        return $this->fetch('', [
            'navTitle' => $navTitle,
            'navSmall' => $navSmall,
            'recordList' => $recordList
        ]);
    }

    //用户删除
    public function del()
    {
        if ($this->request->isPost() && $this->request->isAjax()) {
            $action = $this->request->param('action', '');
            $result = null;
            //用户软删除
            if ($action == 'del') {
                $id = $this->request->param('id', 0);
                $result = model('User.User')->destroy($id);
            }
            if ($action == 'checkboxRemove') {
                $list = $this->request->param();
                $result = model('User.User')->destroy($list['list']);
            }
            //永久删除
            if ($action == 'PMdel') {
                $id = $this->request->param('id', 0);
                $result = model('User.User')->destroy($id, true);
            }
            if ($action == 'PMRemove') {
                $list = $this->request->param();
                $result = model('User.User')->destroy($list['list'], true);
            }
            //积分删除
            $recordRes = null;
            if ($action == 'RecordDel') {
                $id = $this->request->param('id', 0);
                $recordRes = model('Record')->destroy($id);
            }
            if ($action == 'RecordRemove') {
                $list = $this->request->param();
                $recordRes = model('Record')->destroy($list['list']);
            }
            if ($recordRes === FALSE) {
                $this->error(model('Record')->getError());
            } else {
                $this->success('删除成功');
            }
            if ($result === FALSE) {
                $this->error(model('User.User')->getError());
            } else {
                $this->success('删除成功');
            }
        }
    }

    //用户注销
    public function logout()
    {
        cookie('LoginAdmin', null);
        $this->success('退出成功', url('admin/index/login'));
    }
}
